package com.taptech.doufu.util

import android.telephony.PhoneNumberUtils
import android.text.TextUtils
import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
import com.google.i18n.phonenumbers.Phonenumber
import java.util.regex.Matcher
import java.util.regex.Pattern

object CheckInfo {
    fun isEmail(email: String?): Boolean {
        if (null == email || "" == email) return false
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        val p: Pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*") //复杂匹配
        val m: Matcher = p.matcher(email)
        return m.matches()
    }

    fun isMobilPhone(phone: String): Boolean {
        if (TextUtils.isEmpty(phone)) {
            return false
        }
        val regex = "^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}\$"
        return if (phone.length != 11) {
            false
        } else {
            val p = Pattern.compile(regex)
            val m = p.matcher(phone)
            m.matches()
        }
    }

    /**
     * 根据区号判断是否是正确的电话号码
     * @param phoneNumber :带国家码的电话号码
     * @param countryCode :默认国家码
     * return ：true 合法  false：不合法
     */
//    fun isPhoneNumberValid(phoneNumber: String, countryCode: String): Boolean {
//        println("isPhoneNumberValid: $phoneNumber/$countryCode")
//        val phoneUtil: PhoneNumberUtil = PhoneNumberUtil.getInstance()
//        try {
//            val numberProto: Phonenumber.PhoneNumber = phoneUtil.parse(phoneNumber, countryCode)
//            return phoneUtil.isValidNumber(numberProto)
//        } catch (e: NumberParseException) {
//            System.err.println("isPhoneNumberValid NumberParseException was thrown: $e")
//        }
//        return false
//    }
}